#coding:utf8
from config import *
from config_mouse_start import *
from routin_simple import *

def lock_add(jump=True):
    if jump:    
        cfg.lock_step = min( max(cfg.lock_step,lock_step_default) + 1, len(lock_l) -1) 
    else:
        cfg.lock_step = min(cfg.lock_step + 1, len(lock_l) -1) 

def lock_min():        
    cfg.lock_step = max(0, cfg.lock_step - 1)
    
def lock_get():
    if cfg.lock_step==0 and cfg.cur_screen in lock_l_te:
        return lock_l_te[cfg.cur_screen]
    if cfg.lock_step==0:
        return 1
    return round(lock_l[cfg.lock_step%len(lock_l)]*cfg.jump_len)

def xing_mode_enter():    
    if cfg.sum=='0.' or cfg.sum=='0':
        switch_lock_last_move()
        set_cfg_sum_0()
        return
    
    a,b = change_sum(cfg.sum)
    if not a:
        b = -b    
    set_cfg_sum_0()    
    dx,dy = arrm[cfg.lock_xing_last_move]
    do_step_move( round(b*dx*lock_get()), round(b*dy*lock_get()))
    pm.ctrl_co+=1

def xing_rev():
    aa = ('19','28','37','46')
    xx = str(cfg.lock_xing_last_move)            
    for i in aa:
        if xx in i:                    
            if i[0]==xx:
                cfg.lock_xing_last_move=int(i[1])
            else:
                cfg.lock_xing_last_move=int(i[0])      
def xing_alt(idx=0):    
    a = cfg.lock_xing_last_move
    if str(a) in '28':
        cfg.lock_xing_last_move=int('46'[idx])
    if str(a) in '46':
        cfg.lock_xing_last_move=int('82'[idx])
    if str(a) in '19':
        cfg.lock_xing_last_move=int('73'[idx])
    if str(a) in '37':
        cfg.lock_xing_last_move=int('46'[idx])

def deal_xing_mode_2(a):    
    if a==14:
        xing_rev()
        return        
    
    if a==10:
        lock_min()
        return
        
    if a==13:
        lock_add(False)
        return
    
    if a==11:
        cfg.lock_xing_mode=1        
        return
        
    if  a==0 and cfg.sum=='0':   
        if not cfg.xing_chu:
            cfg.xing_chu = True
            return
        cfg.xing_chu = False
        xing_alt()                                                        
        return
    
    if cfg.xing_chu and str(a) in '123456789':
        cfg.xing_chu=False
        if a==5:
            xing_alt(1)
            return
        if str(a) in '12346789':
            cfg.lock_xing_last_move = a
            dx,dy = arrm[cfg.lock_xing_last_move]            
        return
        
    if a >= 1 and a <= 9:
        if cfg.sum == '0':                
            dx,dy = arrm[cfg.lock_xing_last_move]
            do_step_move(a*dx*lock_get(), a*dy*lock_get())
            pm.ctrl_co+=1
            
        elif  cfg.sum=='0.':
            set_cfg_sum_0()                
            dx,dy = arrm[cfg.lock_xing_last_move]
            do_step_move(-a*dx*lock_get(), -a*dy*lock_get()) 
            pm.ctrl_co+=1
        else:
            cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
            return True                           
        set_cfg_sum_0()        
        if cfg.lock_xing_mode_one_time:
            cfg.lock_xing_mode_one_time=False
            cfg.lock_xing_mode = 0    
        return True    
    
def deal_xing_mode_1(a):
    if (a in (10,13) and cfg.sum=='0') or cfg.xing_chu or (cfg.sum=='0' and a==0) or a==14:
        ori = cfg.xing_chu
        deal_xing_mode_2(a)
        return
    
    if a==13:
        xing_rev()
        return        
    
    if a==11:
        cfg.lock_xing_mode=2
        return    
    
    if a >= 0 and a <= 10:
        cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
        return True

def deal_lock_outer(st,a):        
    if a==12:
        deal_minu()
        return
    
    if not ((a>=0 and a<=10) or a in (11,13,14,15)):
        return
    
    if a==15:
        if cfg.lock_xing_mode==2  and cfg.sum[:2]=='0.':
            cfg.sum = cfg.sum.replace('0.0','0.')
            xing_mode_enter()
            return True
        
        if  (cfg.lock_xing_mode!=0 and cfg.sum!='0'):
            xing_mode_enter()
            return True
        cfg.lock_xing_mode = 0        
        cfg.lock = False
        pm.ctrl_has_chu = True
        return True
    
    if cfg.lock_xing_mode==1:     
        deal_xing_mode_1(a)
        return True   
        
    if cfg.lock_xing_mode==2:
        deal_xing_mode_2(a)
        return True
    
    if a == 11:        
        if cfg.lock_xing_mode!=2:
            cfg.lock_xing_mode = 2
        else:
            cfg.lock_xing_mode = 0
        return True
    
    if a == 13:
        if cfg.lock_xing_mode!=1:
            cfg.lock_xing_mode = 1
        else:
            cfg.lock_xing_mode = 0        
        return True
    
    if a == 14:
        if cfg.w2showing and not pm.ctrl_pressed:
            deal_chu()
        else:
            pm.ctrl_has_chu = True
            cfg.sum='0'
        return True
    
    if cfg.lock_xing_mode ==0:
        if  a >= 0 and a <= 10:
            deal_lock(a)
            set_cfg_sum_0()
            return True
        return False                
    
    return False                
def switch_lock_last_move():
    m = {1:9,2:8,3:7,6:4,9:1,8:2,7:3,4:6}
    a = m.get(cfg.lock_xing_last_move,4)
    cfg.lock_xing_last_move = a
def deal_lock(a):    
    if a == 10:
        lock_min()
    if a == 5:
        lock_add()
    if a == 0:
        lock_step_clear()
    if a in arrm:
        dx, dy = arrm[a]        
        do_step_move(dx*lock_get(), dy*lock_get())    
        pm.ctrl_co+=1 
        cfg.last_m = (dx*lock_get(), dy*lock_get())
        cfg.last_m_time = time.time()   
        cfg.last_sum=1
        cfg.lock_xing_last_move =  a        
    